package com.tanhua.manager.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.PublishVO;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private PublishApi publishApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private VideoApi videoApi;

    public ResponseEntity findUserList(Integer page, Integer pagesize) {
        IPage iPage = userInfoApi.findUserList(page, pagesize);
        PageResult<UserInfo> pageResult = new PageResult<>();
        pageResult.setCounts((int) iPage.getTotal());
        pageResult.setPage(page);
        pageResult.setPages((int) iPage.getPages());
        pageResult.setPagesize(pagesize);
        pageResult.setItems(iPage.getRecords());
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity findUserById(Long id) {
        UserInfo userInfo = userInfoApi.findUserInfo(id);
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findPublishList(Integer page, Integer pagesize, Long uid) {
        PageResult pageResult = publishApi.findPublishList(page, pagesize, uid);
        ArrayList<PublishVO> vos = new ArrayList<>();
        List<Publish> items = pageResult.getItems();
        for (Publish publish : items) {
            PublishVO vo = new PublishVO();
            UserInfo userInfo = userInfoApi.findUserInfo(publish.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            if (userInfo.getTags() != null) {
                vo.setTags(userInfo.getTags().split(","));
            }
            BeanUtils.copyProperties(publish, vo);
            vo.setId(publish.getId().toHexString());
            vo.setImageContent(publish.getMedias().toArray(new String[]{}));
            vo.setDistance("50米");
            vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));

            vos.add(vo);
        }
        pageResult.setItems(vos);
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity findPublishById(String publishId) {
        Publish publish = publishApi.findById(publishId);
        PublishVO vo = new PublishVO();
        UserInfo userInfo = userInfoApi.findUserInfo(publish.getUserId());
        BeanUtils.copyProperties(userInfo, vo);
        BeanUtils.copyProperties(publish, vo);
        vo.setId(publish.getId().toHexString());
        vo.setImageContent(publish.getMedias().toArray(new String[]{}));
        vo.setDistance("50米");
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findCommentList(String publishId, int page, int pagesize) {
        PageResult pageResult = commentApi.findComments(publishId, page, pagesize);
        List<Comment> items = pageResult.getItems();
        ArrayList<CommentVO> vos = new ArrayList<>();
        for (Comment comment : items) {
            CommentVO vo = new CommentVO();
            UserInfo userInfo = userInfoApi.findUserInfo(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setContent(comment.getContent());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));
            vos.add(vo);
        }
        pageResult.setItems(vos);

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity findVideoList(Long uid, int page, int pagesize) {
        PageResult pageResult = videoApi.findVideoList(uid, page, pagesize);
        List<Video> items = pageResult.getItems();
        ArrayList<VideoVO> vos = new ArrayList<>();
        for (Video video : items) {
            VideoVO vo = new VideoVO();
            UserInfo userInfo = userInfoApi.findUserInfo(video.getUserId());
            vo.setUserId(userInfo.getId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());

            //封装视频信息
            vo.setId(video.getId().toHexString());
            vo.setCover(video.getPicUrl());
            vo.setVideoUrl(video.getVideoUrl());
            vo.setSignature(video.getText());

            vo.setLikeCount(0);
            vo.setCommentCount(0);

            vos.add(vo);
        }
        pageResult.setItems(vos);
        return ResponseEntity.ok(pageResult);
    }


}
